import { createRouter, createWebHashHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
const loginView = 'LoginView'
// 导入views目录中的所有模块
const modules = import.meta.glob('../views/**/*.vue')

const routes = [
    {
        path: '/',
        name: 'layout',
        component: () => import('@/views/LayoutView.vue'),
        meta: {
            title: "名医在线-后台管理系统",
            isLogin: true
        },
        redirect: '/home',
        children: [
            {
                path: '/home',
                name: 'home',
                component: () => import('@/views/HomeView.vue'),
                meta: {
                    title: "名医在线-管理中心",
                    isLogin: true
                }
            },
            // {
            //     path: '/menu',
            //     name: 'menu',
            //     component:modules['../views/menu/MenuIndex.vue'],
            //     meta: {
            //         title: "名医在线-菜单管理",
            //         isLogin: true
            //     }
            // },
            //     {
            //         path: '/role',
            //         name: 'role',
            //         component: () => import('@/views/role/RoleIndex.vue'),
            //         meta: {
            //             title: "名医在线-角色管理",
            //             isLogin: true
            //         }
            //     },
            //     {
            //         path: '/admin',
            //         name: 'admin',
            //         component: () => import('@/views/admin/AdminIndex.vue'),
            //         meta: {
            //             title: "名医在线-管理员管理",
            //             isLogin: true
            //         }
            //     },
            //     {
            //         path: '/business',
            //         name: 'business',
            //         component: () => import('@/views/business/BusinessIndex.vue'),
            //         meta: {
            //             title: "名医在线-医疗机构管理",
            //             isLogin: true
            //         }
            //     },
            //     {
            //         path: '/banner',
            //         name: 'banner',
            //         component: () => import('@/views/banner/BannerIndex.vue'),
            //         meta: {
            //             title: "名医在线-轮播图管理",
            //             isLogin: true
            //         }
            //     },
            //     {
            //         path: '/staff',
            //         name: 'staff',
            //         component: () => import('@/views/staff/staffIndex.vue'),
            //         meta: {
            //             title: "名医在线-医生管理",
            //             isLogin: true
            //         }
            //     },
            //     {
            //         path: '/log',
            //         name: 'log',
            //         component: () => import('@/views/log/LogIndex.vue'),
            //         meta: {
            //             title: "名医在线-系统操作日志管理",
            //             isLogin: true
            //         }
            //     },
        ]
    },
    {
        path: '/:matchPath(.*)*',
        name: '404',
        component: () => import('@/views/NotFound.vue'),
        meta: {
            title: "名医在线-404资源不存在",
            isLogin: false
        }
    },
    {
        path: '/login',
        name: 'login',
        component: () => import(`@/views/${loginView}.vue`),
        meta: {
            title: "名医在线-管理员登录",
            isLogin: false
        }
    }
]

const router = createRouter({
    routes,
    history: createWebHashHistory()
})

router.beforeEach((to, from, next) => {
    if (to.meta.isLogin) {
        const { token } = JSON.parse(localStorage.getItem('adminLoginInfo') || '{}')
        if (!token) {
            ElMessage.warning('您还没有登录, 请先登录')
            return next('/login')
        }
    }
    next()
});


// 动态注册路由规则
export const addRoutes = () => {
    // 获取之前所有的路由对象, 并移除
    router.getRoutes().forEach(item=>{
        const staticRoutes=['login','layout','home','404']
        if(!staticRoutes.includes(item.name)){
            router.removeRoute(item.name)
        }
    })  
   
    // 通过本地存储获取权限数据
    const { permission } = JSON.parse(localStorage.getItem('adminLoginInfo') || '{}')
    permission.forEach(item => {
        if (item.children) {
            item.children.forEach(val => {
                // 根据url构建组件名称
                if (val.url) {
                    const dirName = val.url.slice(1)
                    const fileName = `${dirName}/${dirName.charAt(0).toUpperCase() + dirName.slice(1)}Index`
                    router.addRoute('layout', {
                        path: val.url,
                        name: dirName,
                        // component: () => import(`@/views/${fileName}.vue`),
                        component: modules[`../views/${fileName}.vue`],
                        meta: {
                            title: val.title,
                            isLogin: true
                        }
                    })
                }
            })
        }
    })
}

export default router